#!/bin/bash
#pMusic - frontend
#GPL - Copyright Sigmund Berglund

export TEXTDOMAIN=pmusic
export OUTPUT_CHARSET=UTF-8

#override main pMusic settings
gettext 'Search' > $WORKDIR/filebrowser_dir
echo "pMusic - $(gettext 'Radio streamer')" > $WORKDIR/window_title

#override settings in pmusicrc to search only radio stations
export SEARCH_MYMUSIC=false
export SEARCH_COLLECTIONS=false
export SEARCH_RADIO=true
export SEARCH_PODCAST=false
export SEARCH_ALBUMART=false
export SEARCH_YOUTUBE=false
export SEARCH_JAMENDO=false
export SEARCH_FILES=false

. $APPDIR/gui_add
. $APPDIR/gui_playlist #svg nowplaying
. $APPDIR/gui_misc -skip_albumart
. $APPDIR/gui_navigation
. $APPDIR/gui_mixer

[ ! -s "$STORAGE_DIR"/index_radio ] && $APPDIR/func_radio

$APPDIR/func_add -add pmusic_radio_country
echo false > $WORKDIR/UPDATE_SOURCELIST
echo '(00:00)' > $WORKDIR/statusbar_playlist #this holds total time of all added tracks to playqueue
echo false > $WORKDIR/UPDATE_GUI

#autoplay if played radio last time
if [ -s $WORKDIR/PLAYLIST.radio ] && [ "`grep "$(<$WORKDIR/PLAYLIST.radio)" "$STORAGE_DIR/index_radio"`" ]; then
	$APPDIR/func_store_playlist &
	sleep 0.2
	cp $WORKDIR/PLAYLIST_ALL.radio $WORKDIR/PLAYLIST_ALL
	cp $WORKDIR/playlist.radio $WORKDIR/playlist
	$APPDIR/func_player -play "$(<$WORKDIR/PLAYLIST_ALL)" -autoplay
fi



add_favorites (){
	IFS='|' read NAME ADR < $HOME/.pmusic/nowplaying
	$APPDIR/func_favorites -add_favorites "$ADR"
}
export -f add_favorites



#get rid of tree header
echo '<tree name="sourcelist" headers-visible="false" fixed-height-mode="true" selection-mode="1" sort-function="1" exported_column="0" '$ST' column-sizing="22|600|150|150|40|60|90||60|80|60|70|" column-visible="true|true|false|false|false|false|false|false|false|false|false|false|false|true|false|false|false|false|false|false|false">' > $WORKDIR/tmp.Radio
echo "$GUI_ADD_LIST" | grep -vF '<tree name="sourcelist"' >> $WORKDIR/tmp.Radio
GUI_ADD_LIST="$(<$WORKDIR/tmp.Radio)"

#read geometry
. $HOME/.pmusic/pmusicrc.Radio

GUI='
<window title="'$(<$WORKDIR/window_title)'" icon-name="pmusic" default_height="'$WINDOW_HEIGHT'" default_width="'$WINDOW_WIDTH'">
<vbox '$ST'>
  <vbox '$SF'>
    <hbox '$SF'>
      '"`grep -B1 -A100 -F 'HISTORY_UNDO' <<< "$GUI_ADD_SEARCH" | grep -B100 -Fm1 '</entry>'`"'
      <button tooltip-text="'$(gettext 'Browse radio stations by country')'">
        <label>'$(gettext 'Country')'</label>
        <action>'$APPDIR'/func_add -add pmusic_radio_country</action>
        <action>refresh:SOURCE</action>
      </button>
      <button tooltip-text="'$(gettext 'Browse radio stations by genre')'">
        <label>'$(gettext 'Genre')'</label>
        <action>'$APPDIR'/func_add -add pmusic_radio_genre</action>
        <action>refresh:SOURCE</action>
      </button>
      <button tooltip-text="'$(gettext 'Add playing radio station to favorites')'">
        <input file>'$APPDIR'/themes/'$THEME'/icon_favorites_add.svg</input>
        <height>22</height>
        <action>add_favorites</action>
        <action>refresh:SOURCE</action>
      </button>
      <button tooltip-text="'$(gettext 'Show favorite stations')'">
        <input file>'$APPDIR'/themes/'$THEME'/icon_favorites.svg</input>
        <height>22</height>
        <action>echo -e "pmusic_favorite_lists pmusic_favorite_marks .Radio|__ '$(gettext 'F A V O R I T E S')' __" > '$WORKDIR'/sourcelist; grep -F "gtk-audio-url" "'$STORAGE_DIR'/favorites" > '$WORKDIR'/tmp_favorites; '$APPDIR'/func_rating -buildlist 1 '$WORKDIR'/tmp_favorites | sort -rnst"|" -k14,14 >> '$WORKDIR'/sourcelist; echo "pmusic_rating|" >> '$WORKDIR'/sourcelist</action>
        <action>refresh:SOURCE</action>
      </button>
    </hbox>
  </vbox>
  <vbox '$ST'>
    '$GUI_ADD_LIST'
  </vbox>
  <vbox '$SF'>
    <hbox '$ST'>
      <hbox '$ST'>
        '$GUI_SVG_NOWPLAYING'
      </hbox>
      <vbox '$SF'>
        <hbox '$SF'>
          <button>
            <input file>'$APPDIR'/themes/'$THEME'/icon_play.svg</input>
            <height>22</height>
            <action>'$APPDIR'/func_player -stop; '$APPDIR'/func_player -play</action>
          </button>
          <button>
            <input file>'$APPDIR'/themes/'$THEME'/icon_stop.svg</input>
            <height>22</height>
            <action>'$APPDIR'/func_player -stop</action>
          </button>
          <button tooltip-text="'$(gettext 'Record radio stream into seperated tracks')'">
            <input file stock="gtk-media-record"></input>
            <action>'$APPDIR'/func_radiohits</action>
          </button>
          <button><input file stock="gtk-preferences"></input><action>'$APPDIR'/func_config -radio</action></button>
        </hbox>
        <hbox height-request="16" '$ST'>
          <text width-request="12" '$SF'><label>""</label></text>
          <pixmap '$SF'>
            <input file>'$APPDIR'/themes/'$THEME'/volume.svg</input>
            <height>14</height>
          </pixmap>
          '$GUI_VOLUME'
        </hbox>
      </vbox>
    </hbox>
  </vbox>
  <vbox visible="false" '$SF'>
    '$GUI_PROGRESSBAR'
    '$GUI_PROGRESS'
  </vbox>
  <vbox '$SF'>
    '$GUI_STATUSBAR'
  </vbox>
</vbox>
<action signal="show">'$APPDIR'/func_mixer -get_levels</action>
<action signal="show">refresh:VOLUME</action>
<action signal="show">echo > '$WORKDIR'/gui_is_running</action>
<action signal="delete-event">cp '$WORKDIR'/PLAYLIST '$WORKDIR'/PLAYLIST.radio; cp '$WORKDIR'/PLAYLIST_ALL '$WORKDIR'/PLAYLIST_ALL.radio; cp '$WORKDIR'/playlist '$WORKDIR'/playlist.radio</action>
<action signal="delete-event">'$APPDIR'/func_config -write_frontend_geometry "'$OVERRIDE_PMUSIC_FRONTEND'"</action>
<action signal="delete-event">'$APPDIR'/func -quit</action>
<action signal="delete-event">exit:Exit</action>
</window>'
